package cn.founder.user.controller;

import cn.founder.basic.exception.BusinessException;
import cn.founder.basic.util.AjaxResult;
import cn.founder.user.dto.LoginDto;
import cn.founder.user.service.ILoginServier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginServier loginServier;

    @PostMapping("/account")
    public AjaxResult accountLogin(@RequestBody LoginDto dto) {
        try {
            Map<String, Object> map = loginServier.accountLogin(dto);
            //返回一个token串和login登录的基本信息 用map集合接收
            return AjaxResult.myself().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("系统繁忙，登录失败，请稍后再试！");
        }
    }

    //微信登录
    @PostMapping("/wechat")
    public AjaxResult wechatLogin(@RequestBody Map<String,String> param) {
        try {
            //不知道到底返回免密登录  还是绑定  让后台返回决定
            return loginServier.wechatLogin(param);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("系统繁忙,请稍后再试");
        }
    }


    //微信绑定流程
    @PostMapping("/binderwechat")
    public AjaxResult binderWechat(@RequestBody Map<String,String> params) {
        try {
            Map<String, Object> map = loginServier.binderWechat(params);
            //返回一个token串和login登录的基本信息 用map集合接收
            return AjaxResult.myself().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("系统繁忙，登录失败，请稍后再试！");
        }
    }
}
